package com.deft.design.prototype;

import com.deft.design.AnswerQuestion;
import com.deft.design.ChoiceQuestion;
import com.deft.design.prototype.util.Topic;
import com.deft.design.prototype.util.TopicRandomUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/**
 * @author zdl
 * @description
 * @date 2022-04-21
 **/
public class QuestionBank implements Cloneable {

    private String candidate;
    private String number;

    private ArrayList<ChoiceQuestion> choiceQuestions = new ArrayList<>();
    private ArrayList<AnswerQuestion> answerQuestions = new ArrayList<>();

    public QuestionBank append(ChoiceQuestion choiceQuestion) {
        choiceQuestions.add(choiceQuestion);
        return this;
    }

    public QuestionBank append(AnswerQuestion answerQuestion) {
        answerQuestions.add(answerQuestion);
        return this;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        QuestionBank questionBank = (QuestionBank) super.clone();
        questionBank.choiceQuestions = (ArrayList<ChoiceQuestion>) choiceQuestions.clone();
        questionBank.answerQuestions = (ArrayList<AnswerQuestion>) answerQuestions.clone();

        // 题目乱序
        Collections.shuffle(questionBank.choiceQuestions);
        Collections.shuffle(questionBank.answerQuestions);

        // 答案乱序
        ArrayList<ChoiceQuestion> choiceQuestions = questionBank.choiceQuestions;;
        for (ChoiceQuestion choiceQuestion : choiceQuestions) {
            Topic random = TopicRandomUtil.random(choiceQuestion.getOption(), choiceQuestion.getKey());
            choiceQuestion.setOption(random.getOption());
            choiceQuestion.setKey(random.getKey());
        }
        return questionBank;
    }

    public void setCandidate(String candidate) {
        this.candidate = candidate;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("考生：" + candidate + "\r\n");
        builder.append("考号").append(number);
        builder.append("\r\n-------------------------------------------\r\n");
        builder.append("一、选择题 \r\n");
        int num = 1;
        for (ChoiceQuestion choiceQuestion : choiceQuestions) {
            builder.append(num).append(". ").append(choiceQuestion.getName()).append("\r\n");
            for (Map.Entry<String, String> entry : choiceQuestion.getOption().entrySet()) {
                builder.append(entry.getKey()).append("、").append(entry.getValue()).append("\r\n");
            }
            builder.append("答案：").append(choiceQuestion.getKey()).append("\r\n");
            num++;
        }
        builder.append("\r\n-------------------------------------------\r\n");
        builder.append("二、问答题 \r\n");
        for (AnswerQuestion answerQuestion : answerQuestions) {
            builder.append(num).append(". ").append(answerQuestion.getName()).append("\r\n");
            builder.append("答案：").append(answerQuestion.getKey()).append("\r\n");
            num++;
        }
        return builder.toString();
    }
}
